package cn.calm.swing.osgi.util;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

public class I18n {
	private static ResourceBundle message = I18n.getResourceBundle(I18n.class);
	private static Map<String, ResourceBundle> resbundles;
	static {
		resbundles = new HashMap<String, ResourceBundle>();
	}

	static public String getMessage(String respkg, String resid) {
		ResourceBundle rsbd = getResourceBundle(respkg);
		return rsbd.getString(resid);
	}

	static public String getMessage(Class<?> cl, String reskey) {
		Package pack = cl.getPackage();
		if (pack == null) {
			return getMessage("messages", reskey);
		}
		String pkg = pack.getName();
		String respkg = pkg + ".messages";
		return getMessage(respkg, reskey);
	}

	static public ResourceBundle getResourceBundle(String pkg) {
		ResourceBundle rsbd = resbundles.get(pkg);
		if (rsbd == null) {
			rsbd = ResourceBundle.getBundle(pkg);
			if (rsbd != null) {
				resbundles.put(pkg, rsbd);
			}
		}
		return rsbd;
	}

	static public ResourceBundle getResourceBundle(Class<?> cl) {
		Package pack = cl.getPackage();
		if (pack == null) {
			return getResourceBundle("messages");
		}
		String pkg = pack.getName();
		String respkg = pkg + ".messages";
		System.out.println(respkg);
		return getResourceBundle(respkg);
	}

	public static void main(String[] args) {
		message.getString("test");
	}
}